// 此文件放全局共用的数据
import { defineStore } from 'pinia'
import { getAgentConfig } from '@app/call-center/apis/client-setting'
export const clientSettingModule = defineStore('clientSetting', {
  state: () => {
    return {
      frontCallConfig: {} as any
    }
  },
  getters: {
    getState(state) {
      return state
    }
  },
  actions: {
    setFrontCallConfig(data: any) {
      this.frontCallConfig = data
    }
  }
})
export const getFrontCallConfig = () => {
  const self = clientSettingModule()
  if (Object.keys(self.frontCallConfig).length <= 0) {
    // 没有取过配置的话，请求一次
    return getAgentConfig({}).then((res: any) => {
      if (res && res.success) {
        self.setFrontCallConfig(res.data.frontCallConfig || {})
      }
      return self.frontCallConfig
    })
  } else {
    return new Promise((resolve) => {
      resolve(self.frontCallConfig)
    })
  }
}
